//
//  ListViewModelCollectionViewModel.m
//  clh
//
//  Created by 曹连华 on 2018/4/11.
//  Copyright © 2018年 曹连华. All rights reserved.
//

#import "LVCollectionViewDataSource.h"
#import "LVBaseCollectionViewCell.h"
#import "LVBaseSectionHeaderFooterReusableView.h"
#define WEAK_SELF __weak typeof(self)weakSelf = self
#define STRONG_SELF __strong typeof(weakSelf)self = weakSelf

@interface LVCollectionViewDataSource ()

@property (nonatomic, weak) id <UICollectionViewDataSource> collectionViewDataSource;


@end

@implementation LVCollectionViewDataSource

- (void)resetDataSource:(id<UICollectionViewDataSource>)dataSource {
    self.collectionViewDataSource = dataSource;
}

#pragma mark - CollectionView - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    if (self.collectionViewDataSource && [self.collectionViewDataSource respondsToSelector:@selector(numberOfSectionsInCollectionView:)]){
        
        return [self.collectionViewDataSource numberOfSectionsInCollectionView:collectionView];
        
    } else if(self.collectionViewDataSource && [self.collectionViewDataSource respondsToSelector:@selector(numberOfSectionsInCollectionView:)]) {
        
        return [self.collectionViewDataSource numberOfSectionsInCollectionView:collectionView];
    
    }
    return self.sections.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if(self.collectionViewDataSource && [self.collectionViewDataSource respondsToSelector:@selector(collectionView:numberOfItemsInSection:)]) {
        
        return [self.collectionViewDataSource collectionView:collectionView numberOfItemsInSection:section];
        
    } else if (self.collectionViewDataSource && [self.collectionViewDataSource respondsToSelector:@selector(collectionView:numberOfItemsInSection:)]) {
        
        return [self.collectionViewDataSource collectionView:collectionView numberOfItemsInSection:section];
    }
    return [self sectionModelAtIndex:section].rows.count;
}

- (LVBaseCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LVBaseCellRowModel *rowModel = [self rowModelAtIndex:indexPath];
    
    if(self.collectionViewDataSource && [self.collectionViewDataSource respondsToSelector:@selector(collectionView:cellForItemAtIndexPath:)]){
        
        return [self.collectionViewDataSource collectionView:collectionView cellForItemAtIndexPath:indexPath];
        
    } else {
        LVBaseCollectionViewCell *cell = (LVBaseCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:rowModel.reuseIdentifier forIndexPath:indexPath];
        if (!cell) {
            if (rowModel.loadType == TVCCellLoadXIB) {
                cell = (LVBaseCollectionViewCell *)[rowModel createCVCCellByXIB];
            } else {
                cell = (LVBaseCollectionViewCell *)[rowModel createCVCCellByCode];
            }
        }
        [cell updateCVCCellWithRowModel:rowModel atIndexPath:indexPath];
        
        return cell;
    }
}

- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath {
    return [self rowModelAtIndex:indexPath].canMove;
}

- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath {
    if (self.moveRowModelBlock) {
        WEAK_SELF;
        self.moveRowModelBlock(^(BOOL operateSuccess) {
            if (operateSuccess) {
                STRONG_SELF;
                LVBaseCellRowModel *sourceRow = [self rowModelAtIndex:sourceIndexPath];
                
                LVBaseCellSectionModel *sourceSectionModel = [self sectionModelAtIndex:sourceIndexPath.section];
                LVBaseCellSectionModel *destinationSectionModel = [self sectionModelAtIndex:destinationIndexPath.section];
                
                [sourceSectionModel.rows removeObjectAtIndex:sourceIndexPath.row];//先把原来位置的 rowModel 删除
                
                if (sourceIndexPath.section == destinationIndexPath.section) {//同组 移动
                    [sourceSectionModel.rows insertObject:sourceRow atIndex:destinationIndexPath.row];
                } else if (sourceSectionModel.canMoveToOtherSection && destinationSectionModel.canMoveToOtherSection) {// 不同组移动
                    [destinationSectionModel.rows insertObject:sourceRow atIndex:destinationIndexPath.row];
                }
            }
        },sourceIndexPath,destinationIndexPath);
    }
    
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

    LVBaseCellSectionModel *sectionModel = [self sectionModelAtIndex:indexPath.section];
     if(self.collectionViewDataSource &&
              [self.collectionViewDataSource respondsToSelector:@selector(collectionView:viewForSupplementaryElementOfKind:atIndexPath:)]) {
        return [self.collectionViewDataSource collectionView:collectionView viewForSupplementaryElementOfKind:kind atIndexPath:indexPath];
    }  else {
        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
            LVBaseSectionHeaderFooterReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:sectionModel.sectionHeader.reuseIdentifier forIndexPath:indexPath];
            [header updateWithHeaderFooterModel:sectionModel.sectionHeader atIndexPath:indexPath];
            return header;

        } else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
            LVBaseSectionHeaderFooterReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:sectionModel.sectionFooter.reuseIdentifier forIndexPath:indexPath];
            [footer updateWithHeaderFooterModel:sectionModel.sectionFooter atIndexPath:indexPath];
            return footer;

        }
    }
    return nil;
}

- (nullable NSArray<NSString *> *)indexTitlesForCollectionView:(UICollectionView *)collectionView {
    if (@available(iOS 14.0, *)) {
        if(self.collectionViewDataSource && [self.collectionViewDataSource respondsToSelector:@selector(indexTitlesForCollectionView:)]) {
            self.sectionIndexTitles = [self.collectionViewDataSource indexTitlesForCollectionView:collectionView];
        }
   }
    return self.sectionIndexTitles;
}

- (NSIndexPath *)collectionView:(UICollectionView *)collectionView indexPathForIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    
    if (@available(iOS 14.0, *)) {
        if(self.collectionViewDataSource && [self.collectionViewDataSource respondsToSelector:@selector(collectionView:indexPathForIndexTitle:atIndex:)]) {
            return [self.collectionViewDataSource collectionView:collectionView indexPathForIndexTitle:title atIndex:index];
        }
    }
    if(self.sectionIndexTitles.count > 0) {
        return [NSIndexPath indexPathForRow:0 inSection:[self.sectionIndexTitles indexOfObject:title]];
    } else {
        return [NSIndexPath indexPathForRow:0 inSection:0];
    }
}

@end
